package com.woniu.common.exception;

import com.woniu.common.core.domain.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

//@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);


//    @ExceptionHandler(Exception.class)
//    @ResponseBody
    public AjaxResult handlerException(Exception e){
        AjaxResult ajaxResult = null;
        if(e instanceof CustomException){
            CustomException customException= (CustomException)e;
            ajaxResult = new AjaxResult(customException.getCode(),customException.getMsg());
            logger.error(customException.getMsg());
        }else{
             ajaxResult = new AjaxResult(500,"服务端异常，请联系管理员");
            logger.error("服务端异常，请联系管理员",e);
        }

        return ajaxResult;
    }
}
